package win32;

import cn.hutool.core.swing.RobotUtil;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * Java 中获取 Windows 打开的其他窗口的大小和坐标
 * https://www.zhihu.com/question/580300402/answer/2858313049
 * https://blog.csdn.net/yhj_911/article/details/104269991
 * https://github.com/java-native-access/jna
 * 码云镜像：https://gitcode.net/mirrors/java-native-access/jna
 * 传奇自动回收脚本 https://www.bilibili.com/read/cv16504988?jump_opus=1
 * Java 通过JNA 调用Windows Api - 自动发送微信消息给指定用户 https://blog.csdn.net/csdn_lrx/article/details/96630776
 * 基于Java实现的定时给微信好友发送消息 https://blog.csdn.net/qq_44085437/article/details/127888757
 * 使用JNA访问WindowsAPI操作Windows窗口元素 https://www.cnblogs.com/yumingle/p/6776258.html
 * Java实现自动输入账号密码登陆软件 https://www.likecs.com/show-203512604.html
 * java 识别桌面应用 自动化测试 自动输入exe程序的账号密码的输入框控件 https://ask.csdn.net/questions/1055282
 * 解决AutoItX4Java报错：ComFailException https://blog.csdn.net/baidu_41778202/article/details/106348462
 */
public class WindowInfo {

    public static void main(String[] args) throws Exception {
        // 打开程序
        Runtime a = Runtime.getRuntime();
        Process p = a.exec("C:\\Windows\\System32\\cmd.exe");
        // Process p = a.exec("C:\\Program Files (x86)\\Sangfor\\SSL\\EasyConnect\\EasyConnect.exe");

        //代码休眠
        Thread.sleep(5000);

        // 获取窗口句柄
        // https://baike.baidu.com/item/findwindow
        // 检索处理顶级窗口的类名和窗口名称匹配指定的字符串
        // HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName);
        // 第一个参数是Windows窗体的窗体类，第二个参数是窗体的标题 2者必须传一个
        // 用标题找到的句柄操作微信，会出现另一个小窗口 难道是有几个标题为微信的窗口
        // 用类名找到的，可以操作

        // WinDef.HWND hwnd = User32.INSTANCE.FindWindow("dlgWindow_1", "EasyConnect");
        // WinDef.HWND hwnd = User32.INSTANCE.FindWindow("dlgWindow_1", null);
        // System.out.println(hwnd.toString()); // native@0x120f0e

        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "微信");
        hwnd = User32.INSTANCE.FindWindow("WeChatMainWndForPC", null);
        System.out.println(hwnd.toString()); // native@0x70764

        // cmd窗口
        // WinDef.HWND hwnd = User32.INSTANCE.FindWindow("ConsoleWindowClass", null);

        if (hwnd != null) {
            // 获取窗口大小
            WinDef.RECT rect = new WinDef.RECT();
            User32.INSTANCE.GetWindowRect(hwnd, rect);
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            System.out.println("窗口大小: " + width + "x" + height);

            // 获取窗口坐标
            WinDef.POINT point = new WinDef.POINT();
            User32.INSTANCE.GetCursorPos(point);
            System.out.println("窗口坐标: (" + point.x + ", " + point.y + ")");

            // 窗口前置
            // 设置指定窗口的显示状态 https://baike.baidu.com/item/ShowWindow
            // boolean ShowWindow(HWND hWnd, int nCmdShow)
            // 激活并显示窗口 nCmdShow=9
            // 如果窗口之前可见，则返回值为非零。如果窗口之前被隐藏，则返回值为零
            boolean b = User32.INSTANCE.ShowWindow(hwnd, 9);// SW_RESTORE
            System.out.println("ShowWindow::" + b);

            // https://baike.baidu.com/item/SetForegroundWindow
            // 将创建指定窗口的线程设置到前台，并且激活该窗口
            boolean b1 = User32.INSTANCE.SetForegroundWindow(hwnd);// bring to front
            System.out.println("SetForegroundWindow::" + b1);

            // 把窗口移动到0,0
            User32.INSTANCE.MoveWindow(hwnd, 0, 0, width, height, true);
            // 以下是点击
            // moveAndClick(610,670,400);

        } else {
            System.out.println("找不到窗口");
        }

        // EnumWindows();
    }

    private static void moveAndClick(int x,int y,int delay) {
        RobotUtil.mouseMove(x, y);
        RobotUtil.setDelay(delay);
        RobotUtil.click();
    }

    /**
     * Java JNA 聚焦特定窗口
     * https://qa.1r1g.com/sf/ask/2477565051/
     */
    public static void EnumWindows() {
        final User32 user32 = User32.INSTANCE;
        user32.EnumWindows(new WinUser.WNDENUMPROC() {
            int count = 0;

            public boolean callback(WinDef.HWND hWnd, Pointer arg1) {
                char[] windowText = new char[512];
                user32.GetWindowText(hWnd, windowText, 512);
                String wText = Native.toString(windowText);

                // get rid of this if block if you want all windows regardless
                // of whether
                // or not they have text
                if (wText.isEmpty()) {
                    return true;
                }

                System.out.println("Found window with text " + hWnd
                        + ", total " + ++count + " Text: " + wText);
                // if (wText.equals("微信")) {
                //     user32.SetForegroundWindow(hWnd);
                //     return false;
                // }
                return true;
            }
        }, null);
        // user32.SetFocus(hWnd);
        // try {
        //     Robot r = new Robot();
        //     r.keyPress(KeyEvent.VK_ENTER);
        //     r.keyRelease(KeyEvent.VK_ENTER);
        //     r.keyPress(KeyEvent.VK_ENTER);
        //     r.keyRelease(KeyEvent.VK_ENTER);
        //     r.keyPress(KeyEvent.VK_ENTER);
        //     r.keyRelease(KeyEvent.VK_ENTER);
        //     r.keyPress(KeyEvent.VK_ENTER);
        //     r.keyRelease(KeyEvent.VK_ENTER);
        // } catch (AWTException e) {
        //     // TODO Auto-generated catch block
        //     e.printStackTrace();
        // }
    }

}
